package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionExceptionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.InspectionException;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Inspection entities
 * 
 */

@Service("InspectionService")

@Transactional
public class InspectionServiceImpl implements InspectionService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages InspectionException entities
	 * 
	 */
	@Autowired
	private InspectionExceptionDAO inspectionExceptionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new InspectionServiceImpl.
	 *
	 */
	public InspectionServiceImpl() {
	}

	/**
	 * Return a count of all Inspection entity
	 * 
	 */
	@Transactional
	public Integer countInspections() {
		return ((Long) inspectionDAO.createQuerySingleResult("select count(o) from Inspection o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing PigHouse entity
	 * 
	 */
	@Transactional
	public Inspection saveInspectionPigHouse(Integer id, PigHouse related_pighouse) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(id, -1, -1);
		PigHouse existingpigHouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigHouse != null) {
			existingpigHouse.setId(related_pighouse.getId());
			existingpigHouse.setNumber(related_pighouse.getNumber());
			existingpigHouse.setRfid(related_pighouse.getRfid());
			existingpigHouse.setMemo(related_pighouse.getMemo());
			related_pighouse = existingpigHouse;
		}

		inspection.setPigHouse(related_pighouse);
		related_pighouse.getInspections().add(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		return inspection;
	}

	/**
	 * Load an existing Inspection entity
	 * 
	 */
	@Transactional
	public Set<Inspection> loadInspections() {
		return inspectionDAO.findAllInspections();
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Inspection deleteInspectionEmployee(Integer inspection_id, Integer related_employee_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(inspection_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		inspection.setEmployee(null);
		related_employee.getInspections().remove(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return inspection;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Inspection saveInspectionEmployee(Integer id, Employee related_employee) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		inspection.setEmployee(related_employee);
		related_employee.getInspections().add(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return inspection;
	}

	/**
	 * Save an existing InspectionException entity
	 * 
	 */
	@Transactional
	public Inspection saveInspectionInspectionExceptions(Integer id, InspectionException related_inspectionexceptions) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(id, -1, -1);
		InspectionException existinginspectionExceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspectionExceptions != null) {
			existinginspectionExceptions.setId(related_inspectionexceptions.getId());
			existinginspectionExceptions.setMemo(related_inspectionexceptions.getMemo());
			related_inspectionexceptions = existinginspectionExceptions;
		}

		related_inspectionexceptions.setInspection(inspection);
		inspection.getInspectionExceptions().add(related_inspectionexceptions);
		related_inspectionexceptions = inspectionExceptionDAO.store(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		return inspection;
	}

	/**
	 * Delete an existing Inspection entity
	 * 
	 */
	@Transactional
	public void deleteInspection(Inspection inspection) {
		inspectionDAO.remove(inspection);
		inspectionDAO.flush();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Inspection deleteInspectionTask(Integer inspection_id, Integer related_task_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(inspection_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		inspection.setTask(null);
		related_task.getInspections().remove(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return inspection;
	}

	/**
	 * Delete an existing PigHouse entity
	 * 
	 */
	@Transactional
	public Inspection deleteInspectionPigHouse(Integer inspection_id, Integer related_pighouse_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(inspection_id, -1, -1);
		PigHouse related_pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		inspection.setPigHouse(null);
		related_pighouse.getInspections().remove(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		pigHouseDAO.remove(related_pighouse);
		pigHouseDAO.flush();

		return inspection;
	}

	/**
	 */
	@Transactional
	public Inspection findInspectionByPrimaryKey(Integer id) {
		return inspectionDAO.findInspectionByPrimaryKey(id);
	}

	/**
	 * Delete an existing InspectionException entity
	 * 
	 */
	@Transactional
	public Inspection deleteInspectionInspectionExceptions(Integer inspection_id, Integer related_inspectionexceptions_id) {
		InspectionException related_inspectionexceptions = inspectionExceptionDAO.findInspectionExceptionByPrimaryKey(related_inspectionexceptions_id, -1, -1);

		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(inspection_id, -1, -1);

		related_inspectionexceptions.setInspection(null);
		inspection.getInspectionExceptions().remove(related_inspectionexceptions);

		inspectionExceptionDAO.remove(related_inspectionexceptions);
		inspectionExceptionDAO.flush();

		return inspection;
	}

	/**
	 * Save an existing Inspection entity
	 * 
	 */
	@Transactional
	public void saveInspection(Inspection inspection) {
		Inspection existingInspection = inspectionDAO.findInspectionByPrimaryKey(inspection.getId());

		if (existingInspection != null) {
			if (existingInspection != inspection) {
				existingInspection.setId(inspection.getId());
				existingInspection.setTime(inspection.getTime());
				existingInspection.setMemo(inspection.getMemo());
			}
			inspection = inspectionDAO.store(existingInspection);
		} else {
			inspection = inspectionDAO.store(inspection);
		}
		inspectionDAO.flush();
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Inspection saveInspectionTask(Integer id, Task related_task) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		inspection.setTask(related_task);
		related_task.getInspections().add(inspection);
		inspection = inspectionDAO.store(inspection);
		inspectionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return inspection;
	}

	/**
	 * Return all Inspection entity
	 * 
	 */
	@Transactional
	public List<Inspection> findAllInspections(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Inspection>(inspectionDAO.findAllInspections(startResult, maxRows));
	}
}
